#include "RibbonLeTurnLampState.h"
#include "F3LampInterfaces.h"
#include "LampManager.h"
#include "Animations.h"

RibbonLeTurnLampStateMachine g_RibbonLeTurnLampStateMachine;
void InitRibbonLeTurnLampStateMachine(void)
{
    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFree_.sm_.Entry = RibbonLeTurnLampStateMachineFreeEntry;
    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFree_.sm_.Excute = RibbonLeTurnLampStateMachineFreeExcute;
    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFree_.sm_.Exit = RibbonLeTurnLampStateMachineFreeExit;    
    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFree_.sm_.waitExitCount_ = 0;

    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFlicker_.sm_.Entry = RibbonLeTurnLampStateMachineFlickerEntry;
    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFlicker_.sm_.Excute = RibbonLeTurnLampStateMachineFlickerExcute;
    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFlicker_.sm_.Exit = RibbonLeTurnLampStateMachineFlickerExit;
    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFlicker_.sm_.waitExitCount_ = 0;

    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFlow_.sm_.Entry = RibbonLeTurnLampStateMachineFlowEntry;
    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFlow_.sm_.Excute = RibbonLeTurnLampStateMachineFlowExcute;
    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFlow_.sm_.Exit = RibbonLeTurnLampStateMachineFlowExit;
    g_RibbonLeTurnLampStateMachine.ribbonLeTurnLampStateMachineFlow_.sm_.waitExitCount_ = 0;

    g_RibbonLeTurnLampStateMachine.sm_.Entry = RibbonLeTurnStateMachineEntry;
    g_RibbonLeTurnLampStateMachine.sm_.Excute = RibbonLeTurnStateMachineExcute;
    g_RibbonLeTurnLampStateMachine.sm_.Exit = RibbonLeTurnStateMachineExit;
    g_RibbonLeTurnLampStateMachine.sm_.waitExitCount_ = 0;
}

void RibbonLeTurnLampStateMachineFreeEntry(void)
{
    // std::cout << "Base::RibbonLeTurnLampStateMachineFreeEntry" << std::endl;
    trenLeFrntTurnLampOff(&g_LeFrntTrunLampView.model_->mask, &g_LeFrntTrunLampView.model_->pwm_);
}

void RibbonLeTurnLampStateMachineFreeExcute(void)
{
    // std::cout << "Base::RibbonLeTurnLampStateMachineFreeExcute" << std::endl;
}

void RibbonLeTurnLampStateMachineFreeExit(void)
{
    // std::cout << "Base::RibbonLeTurnLampStateMachineFreeExit" << std::endl;
}

void RibbonLeTurnLampStateMachineFlickerEntry(void)
{
    // std::cout << "Base::RibbonLeTurnLampStateMachineFlickerEntry" << std::endl;
}

void RibbonLeTurnLampStateMachineFlickerExcute(void)
{
    // std::cout << "Base::RibbonLeTurnLampStateMachineFlickerExcute" << std::endl;
    // buildLeTurnLamp_TL_EN_FaultFlag(&g_LeFrntTrunLampView.lastModel_.TL_EN_FaultFlag_, &g_LeFrntTrunLampView.model_->TL_EN_FaultFlag_);  //dev support
    playLeFrntTurnLampFlickerAnimation(&g_LeFrntTrunLampView.model_->mask, &g_LeFrntTrunLampView.model_->pwm_);
}

void RibbonLeTurnLampStateMachineFlickerExit(void)
{
    // std::cout << "Base::RibbonLeTurnLampStateMachineFlickerExit" << std::endl;
    trenLeFrntTurnLampOff(&g_LeFrntTrunLampView.model_->mask, &g_LeFrntTrunLampView.model_->pwm_);
}

void RibbonLeTurnLampStateMachineFlowEntry(void)
{
    // std::cout << "Base::RibbonLeTurnLampStateMachineFlowEntry" << std::endl;
}

void RibbonLeTurnLampStateMachineFlowExcute(void)
{
    // std::cout << "Base::RibbonLeTurnLampStateMachineFlowExcute" << std::endl;
    // buildLeTurnLamp_TL_EN_FaultFlag(&g_LeFrntTrunLampView.lastModel_.TL_EN_FaultFlag_, &g_LeFrntTrunLampView.model_->TL_EN_FaultFlag_);  //dev support
    playLeFrntTurnLampFlowAnimation(&g_LeFrntTrunLampView.model_->mask, &g_LeFrntTrunLampView.model_->pwm_);
}

void RibbonLeTurnLampStateMachineFlowExit(void)
{
    // std::cout << "Base::RibbonLeTurnLampStateMachineFlowExit" << std::endl;
    trenLeFrntTurnLampOff(&g_LeFrntTrunLampView.model_->mask, &g_LeFrntTrunLampView.model_->pwm_);
}

static void RibbonLeTurnLampStateExitWithState(RibbonLeTurnLampStateMachine* normalStateMachine, RibbonLeTurnLampState inputState)
{
    switch(inputState)
    {
        case RibbonLeTurnLamp_Free:
        {
            normalStateMachine->ribbonLeTurnLampStateMachineFree_.sm_.Exit();
            break;
        }

        case RibbonLeTurnLamp_Flicker:
        {
            normalStateMachine->ribbonLeTurnLampStateMachineFlicker_.sm_.Exit();
            break;
        }

        case RibbonLeTurnLamp_Flow:
        {
            normalStateMachine->ribbonLeTurnLampStateMachineFlow_.sm_.Exit();
            break;
        }

        default:
        {
            break;
        }
    }
}

static void RibbonLeTurnLampStateEntryWithState(RibbonLeTurnLampStateMachine* normalStateMachine, RibbonLeTurnLampState inputState)
{
    switch(inputState)
    {
        case RibbonLeTurnLamp_Free:
        {
            normalStateMachine->ribbonLeTurnLampStateMachineFree_.sm_.Entry();
            break;
        }

        case RibbonLeTurnLamp_Flicker:
        {
            normalStateMachine->ribbonLeTurnLampStateMachineFlicker_.sm_.Entry();
            break;
        }

        case RibbonLeTurnLamp_Flow:
        {
            normalStateMachine->ribbonLeTurnLampStateMachineFlow_.sm_.Entry();
            break;
        }

        default:
        {
            break;
        }
    }
}

static void RibbonLeTurnLampStateExcuteWithState(RibbonLeTurnLampStateMachine* normalStateMachine, RibbonLeTurnLampState inputState)
{
    switch(inputState)
    {
        case RibbonLeTurnLamp_Free:
        {
            normalStateMachine->ribbonLeTurnLampStateMachineFree_.sm_.Excute();
            break;
        }

        case RibbonLeTurnLamp_Flicker:
        {
            normalStateMachine->ribbonLeTurnLampStateMachineFlicker_.sm_.Excute();
            break;
        }

        case RibbonLeTurnLamp_Flow:
        {
            normalStateMachine->ribbonLeTurnLampStateMachineFlow_.sm_.Excute();
            break;
        }

        default:
        {
            break;
        }
    }
}

void RibbonLeTurnStateMachineEntry(void)
{
    // std::cout << "RibbonLeTurnStateMachineEntry" << std::endl;
}

void RibbonLeTurnStateMachineExcute(void)
{
    // std::cout << "RibbonLeTurnStateMachineExcute" << std::endl;
    if(g_TurnLastState.ribbonLeTurnLampState_ != g_TurnNewState.ribbonLeTurnLampState_)
    {
        RibbonLeTurnLampStateExitWithState(&g_RibbonLeTurnLampStateMachine, g_TurnLastState.ribbonLeTurnLampState_);
        RibbonLeTurnLampStateEntryWithState(&g_RibbonLeTurnLampStateMachine, g_TurnNewState.ribbonLeTurnLampState_);
    }
    RibbonLeTurnLampStateExcuteWithState(&g_RibbonLeTurnLampStateMachine, g_TurnNewState.ribbonLeTurnLampState_);

    UpdateRibbonLeTurnLampState(g_TurnNewState.ribbonLeTurnLampState_, &g_TurnLastState.ribbonLeTurnLampState_);
}

void RibbonLeTurnStateMachineExit(void)
{
    // std::cout << "RibbonLeTurnStateMachineExit" << std::endl;
    RibbonLeTurnLampStateExitWithState(&g_RibbonLeTurnLampStateMachine, g_TurnNewState.ribbonLeTurnLampState_);
    UpdateRibbonLeTurnLampState(RibbonLeTurnLamp_Invaild, &g_TurnLastState.ribbonLeTurnLampState_);
}
